<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付单号" prop="paymentNo">
        <el-input
          v-model="queryParams.paymentNo"
          placeholder="请输入支付单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="平台支付ID" prop="platformPaymentId">
        <el-input
          v-model="queryParams.platformPaymentId"
          placeholder="请输入平台支付ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-select
          v-model="queryParams.paymentMethod"
          placeholder="请选择支付方式"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CROSSBORDER_PAYMENT_METHOD)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付渠道" prop="paymentChannel">
        <el-input
          v-model="queryParams.paymentChannel"
          placeholder="请输入支付渠道"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付服务商" prop="paymentProvider">
        <el-input
          v-model="queryParams.paymentProvider"
          placeholder="请输入支付服务商"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付类型" prop="paymentType">
        <el-select
          v-model="queryParams.paymentType"
          placeholder="请选择支付类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_PAYMENT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付金额" prop="paymentAmount">
        <el-input
          v-model="queryParams.paymentAmount"
          placeholder="请输入支付金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付货币" prop="currencyCode">
        <el-input
          v-model="queryParams.currencyCode"
          placeholder="请输入支付货币"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="汇率" prop="exchangeRate">
        <el-input
          v-model="queryParams.exchangeRate"
          placeholder="请输入汇率"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="基础货币金额" prop="baseAmount">
        <el-input
          v-model="queryParams.baseAmount"
          placeholder="请输入基础货币金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手续费" prop="feeAmount">
        <el-input
          v-model="queryParams.feeAmount"
          placeholder="请输入手续费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="网关费用" prop="gatewayFee">
        <el-input
          v-model="queryParams.gatewayFee"
          placeholder="请输入网关费用"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="处理费用" prop="processingFee">
        <el-input
          v-model="queryParams.processingFee"
          placeholder="请输入处理费用"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="净收金额" prop="netAmount">
        <el-input
          v-model="queryParams.netAmount"
          placeholder="请输入净收金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="paymentStatus">
        <el-select
          v-model="queryParams.paymentStatus"
          placeholder="请选择支付状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_PAYMENT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="交易ID" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入交易ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="授权码" prop="authorizationCode">
        <el-input
          v-model="queryParams.authorizationCode"
          placeholder="请输入授权码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="参考号" prop="referenceNo">
        <el-input
          v-model="queryParams.referenceNo"
          placeholder="请输入参考号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="paymentTime">
        <el-date-picker
          v-model="queryParams.paymentTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker
          v-model="queryParams.expireTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="回调时间" prop="callbackTime">
        <el-date-picker
          v-model="queryParams.callbackTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="回调数据JSON" prop="callbackData">
        <el-input
          v-model="queryParams.callbackData"
          placeholder="请输入回调数据JSON"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="风险评分" prop="riskScore">
        <el-input
          v-model="queryParams.riskScore"
          placeholder="请输入风险评分"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="风险等级" prop="riskLevel">
        <el-select
          v-model="queryParams.riskLevel"
          placeholder="请选择风险等级"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_RISK_LEVEL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="反欺诈检查结果JSON" prop="fraudCheckResult">
        <el-input
          v-model="queryParams.fraudCheckResult"
          placeholder="请输入反欺诈检查结果JSON"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付信息JSON" prop="paymentInfo">
        <el-input
          v-model="queryParams.paymentInfo"
          placeholder="请输入支付信息JSON"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="错误代码" prop="errorCode">
        <el-input
          v-model="queryParams.errorCode"
          placeholder="请输入错误代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="错误信息" prop="errorMessage">
        <el-input
          v-model="queryParams.errorMessage"
          placeholder="请输入错误信息"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="重试次数" prop="retryCount">
        <el-input
          v-model="queryParams.retryCount"
          placeholder="请输入重试次数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['crossborder:order-payment:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['crossborder:order-payment:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['crossborder:order-payment:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column label="支付编号" align="center" prop="id" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="支付单号" align="center" prop="paymentNo" />
      <el-table-column label="平台支付ID" align="center" prop="platformPaymentId" />
      <el-table-column label="支付方式" align="center" prop="paymentMethod" />
      <el-table-column label="支付渠道" align="center" prop="paymentChannel" />
      <el-table-column label="支付服务商" align="center" prop="paymentProvider" />
      <el-table-column label="支付类型" align="center" prop="paymentType" />
      <el-table-column label="支付金额" align="center" prop="paymentAmount" />
      <el-table-column label="支付货币" align="center" prop="currencyCode" />
      <el-table-column label="汇率" align="center" prop="exchangeRate" />
      <el-table-column label="基础货币金额" align="center" prop="baseAmount" />
      <el-table-column label="手续费" align="center" prop="feeAmount" />
      <el-table-column label="网关费用" align="center" prop="gatewayFee" />
      <el-table-column label="处理费用" align="center" prop="processingFee" />
      <el-table-column label="净收金额" align="center" prop="netAmount" />
      <el-table-column label="支付状态" align="center" prop="paymentStatus" />
      <el-table-column label="交易ID" align="center" prop="transactionId" />
      <el-table-column label="授权码" align="center" prop="authorizationCode" />
      <el-table-column label="参考号" align="center" prop="referenceNo" />
      <el-table-column
        label="支付时间"
        align="center"
        prop="paymentTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="过期时间"
        align="center"
        prop="expireTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="回调时间"
        align="center"
        prop="callbackTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="回调数据JSON" align="center" prop="callbackData" />
      <el-table-column label="风险评分" align="center" prop="riskScore" />
      <el-table-column label="风险等级" align="center" prop="riskLevel" />
      <el-table-column label="反欺诈检查结果JSON" align="center" prop="fraudCheckResult" />
      <el-table-column label="支付信息JSON" align="center" prop="paymentInfo" />
      <el-table-column label="错误代码" align="center" prop="errorCode" />
      <el-table-column label="错误信息" align="center" prop="errorMessage" />
      <el-table-column label="重试次数" align="center" prop="retryCount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crossborder:order-payment:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crossborder:order-payment:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrderPaymentForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrderPaymentApi, OrderPayment } from '@/api/crossborder/orderpayment'
import OrderPaymentForm from './OrderPaymentForm.vue'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'

/** 跨境电商订单支付 列表 */
defineOptions({ name: 'OrderPayment' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OrderPayment[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderId: undefined,
  orderNo: undefined,
  paymentNo: undefined,
  platformPaymentId: undefined,
  paymentMethod: undefined,
  paymentChannel: undefined,
  paymentProvider: undefined,
  paymentType: undefined,
  paymentAmount: undefined,
  currencyCode: undefined,
  exchangeRate: undefined,
  baseAmount: undefined,
  feeAmount: undefined,
  gatewayFee: undefined,
  processingFee: undefined,
  netAmount: undefined,
  paymentStatus: undefined,
  transactionId: undefined,
  authorizationCode: undefined,
  referenceNo: undefined,
  paymentTime: [],
  expireTime: [],
  callbackTime: [],
  callbackData: undefined,
  riskScore: undefined,
  riskLevel: undefined,
  fraudCheckResult: undefined,
  paymentInfo: undefined,
  errorCode: undefined,
  errorMessage: undefined,
  retryCount: undefined,
  remark: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderPaymentApi.getOrderPaymentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrderPaymentApi.deleteOrderPayment(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除跨境电商订单支付 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await OrderPaymentApi.deleteOrderPaymentList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: OrderPayment[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrderPaymentApi.exportOrderPayment(queryParams)
    download.excel(data, '跨境电商订单支付.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>